package com.rui.common.base;

import com.rui.common.util.Pages;
import com.rui.common.util.ValidatorUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * service基类
 *
 * @author xr
 * @email xr@qq.com
 * @date 2020-01-01 00:00:00
 */
@Transactional(rollbackFor = {Exception.class})
public abstract class BaseService<D extends BaseDao<T>, T extends BaseEntity> {
    @Autowired
    protected D dao;

    /**
     * 通过id获取对象
     *
     * @param id
     * @return
     */
    public T get(Long id) {
        ValidatorUtil.isNull(id, "id is null");
        return dao.get(id);
    }

    /**
     * 通过对象获取对象
     *
     * @param entity
     * @return
     */
    public T getByEntity(T entity) {
        ValidatorUtil.isNull(entity, "obj is null");
        return dao.getByEntity(entity);
    }

    /**
     * 通过entity获取list
     *
     * @param entity
     * @return
     */
    public List<T> selectList(T entity) {
        return dao.selectList(entity);
    }

    /**
     * 通过map获取list
     *
     * @param map
     * @return
     */
    public List<T> findList(Map<String, Object> map) {
        return dao.findList(map);
    }

    /**
     * 通过map分页查询
     *
     * @param map
     * @return
     */
    public Pages findPage(Map<String, Object> map) {
        List<T> list = dao.findList(map);
        return new Pages(list);
    }

    /**
     * 保存
     *
     * @param entity
     * @return
     */
    public int save(T entity) {
        ValidatorUtil.isNull(entity, "obj is null");
        if (entity.getId() == null) {
            entity.prepareInsert();
            return dao.insert(entity);
        } else {
            entity.prepareUpdate();
            return dao.update(entity);
        }
    }

    /**
     * 批量保存
     *
     * @param list
     * @return
     */
    @Transactional(rollbackFor = {Exception.class})
    public int saveList(List<T> list) {
        List<T> insertList = new ArrayList<>();
        List<T> updateList = new ArrayList<>();
        for (T entity : list) {
            ValidatorUtil.isNull(entity, "item is null");
            if (entity.getId() == null) {
                entity.prepareInsert();
                insertList.add(entity);
            } else {
                entity.prepareUpdate();
                updateList.add(entity);
            }
        }
        int index = 0;
        if (CollectionUtils.isNotEmpty(insertList)) {
            index += dao.insertList(insertList);
        }
        if (CollectionUtils.isNotEmpty(updateList)) {
            index += dao.updateList(updateList);
        }
        return index;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    public int delete(Long id) {
        ValidatorUtil.isNull(id, "id is null");
        return dao.delete(id);
    }

    /**
     * 通过对象删除
     *
     * @param entity
     * @return
     */
    public int deleteByEntity(T entity) {
        ValidatorUtil.isNull(entity, "obj is null");
        return dao.deleteByEntity(entity);
    }

    /**
     * 通过ids批量删除
     *
     * @param ids
     * @return
     */
    public int deleteArray(Long[] ids) {
        ValidatorUtil.isEmpty(ids, "ids is null");
        return dao.deleteArray(ids);
    }

}
